import axios from 'axios'
import http from '@/utils/http'
import type { PluginDTO } from '@/types/plugins'

const BASE_URL = '/plugins'

export interface PluginQuery {
  name?: string
  type?: string
  sort: Array<string>
}

// export interface PluginPageQuery extends PluginQuery, PageQuery {}

/** 系统字典类型 */
export interface PluginItem {
  id: string
  title: string
  core: string
  description: string
  name?: string
  actionType?: string
  infos?: any
  enabled?: boolean
  isDataSource?: boolean
  expires?: boolean
  hasUpdate?: boolean
}
export interface MicroApp {
  name: string
  entry: string
  activeRule: string
}
export function GetMicroApp() {
  return http.get<MicroApp[]>(`/micro-apps`)
}
/** @desc 查询R插件列表 */
export function queryPluginsList() {
  return http.get<PluginDTO[]>(`${BASE_URL}`)
}

/** @desc 查找所有插件 从官网查询   */
export function queryAllPluginsList() {
  return http.get<PluginDTO[]>('/proxy/api/plugin')
}

/** @desc 查询插件信息 （从官网查询） */
export function queryPluginFromSite(id: string) {
  return http.get(`/proxy/api/plugin/${id}`)
}
/** @desc 查询插件信息 （从官网查询） */
export function queryPluginVersionFromSite(id: string) {
  return axios.get(`${import.meta.env.VITE_API_PREFIX ?? import.meta.env.VITE_API_BASE_URL}/proxy/api/plugin/${id}/versions`)
}

/** @desc 查询插件信息（本机） */
export function queryPlugin(id: string) {
  return http.get(`${BASE_URL}/${id}`)
}

/** 插件卸载 */
export function uninstall(id: string) {
  return http.post(`${BASE_URL}/${id}/uninstall`)
}
export function install(id: string, version: string) {
  return http.post(`${BASE_URL}/${id}/${version}/install`, {}, { timeout: 0 })
}
